﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Data.Util;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 大屏管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.11.28 
    /// </summary>
    public class DataService : DataFactory, DataIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        public async Task<PageResult<DataEntity>> GetList(DataListQuery input)
        {
            using (var db = this.Basic)
            {
                var queryWhere = ExtLinq.True<DataEntity>();
                var queryParams = input.queryJson.ToObject();
                //分类id
                if (input.categoryId != null && !string.IsNullOrEmpty(input.categoryId.ToString()) && input.categoryId.ToString() != "0")
                {
                    queryWhere = queryWhere.And(m => m.CategoryId == input.categoryId);
                }
                //关键字（名称、编码）
                if (!string.IsNullOrEmpty(input.keyword))
                {
                    queryWhere = queryWhere.And(m => m.FullName.Contains(input.keyword) || m.EnCode.Contains(input.keyword));
                }
                return await db.GetIQueryable<DataEntity>().Where(queryWhere).OrderBy(t => t.SortCode).GetPageResultAsync(input);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public async Task<DataEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<DataEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        ///  <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Create(DataEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.InsertAsync(entity);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        ///  <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Update(DataEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public async Task<int> Delete(DataEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.DeleteAsync(entity);
            }
        }
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<DataEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<DataEntity>().OrderBy(o => o.SortCode).ToListAsync();
            }
        }
    }
}
